package com.cssw.fyzb.busi.api.pay;

import com.cssw.fyzb.busi.config.TpsClient;
import com.cssw.fyzb.busi.model.tps.*;
import com.cssw.fyzb.busi.service.pay.QuickPayService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import com.ydn.simplelock.SimpleLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2020/7/14 22:10
 */

@Function(description = "快捷签约信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "orderNo", type = Type.String, description = "订单号", required = true)
})
@Component
public class QuickPayQuery implements Action {

    private static final Logger logger = LoggerFactory.getLogger(QuickPayQuery.class);

    @Autowired
    private TpsClient tpsClient;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        String orderNo = request.getString("orderNo");
        Map map = new HashMap();
        try {
            Tf56PayQuickPayQueryVo vo = orderQuery(orderNo);
            if (null == vo) {
                return Response.fail("操作失败，请重试！");
            }

            Tf56PayQuickPayQueryDataVo dataVo = vo.getDataVo();
            if (null == dataVo) {
                return Response.fail("操作失败，请重试！");
            }

            //status： 1-处理中 2-成功 3-失败

            //已申请/处理中/成功/失败/关闭/已退款
            if ("成功".equals(dataVo.getStatus())) {
                map.put("status", "2");
                return Response.success("支付成功").put("data", map);
            } else if ("已申请".equals(dataVo.getStatus())
                    || "处理中".equals(dataVo.getStatus())) {
                map.put("status", "1");
                return Response.success("支付中").put("data", map);
            } else {
                map.put("status", "3");
                String errorMsg = null; //dataVo.getRemark();
                if (StringUtil.isEmpty(errorMsg)){
                    errorMsg = "操作失败，如已支付请联系客服";
                }
                return Response.fail(errorMsg).put("data", map);
            }
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("支付失败！").put("data", map);
        }
    }

    private Tf56PayQuickPayQueryVo orderQuery(String tradeNo) {
        Request request = new Request("Tf56PayQuickPayQuery");
        request.setParameter("orderNo", tradeNo);
        Response response = tpsClient.send(request);
        if (!response.isSuccess()) {
            throw new BizException(response.getCode(), response.getMessage());
        }
        return response.getData().get("data", Tf56PayQuickPayQueryVo.class);

    }

    private boolean isSuccess(Tf56PayQuickSignVo vo) {
        return "GP_00".equals(vo.getCode()) && "GPBIZ_00".equals(vo.getBiz_code());
    }

    private String getErrorCode(Tf56PayQuickSignVo vo) {
        if (!"GP_00".equals(vo.getCode())) {
            return vo.getCode();
        }
        if (!"GPBIZ_00".equals(vo.getBiz_code())) {
            return vo.getBiz_code();
        }

        return "0";
    }

    private String getErrorMsg(Tf56PayQuickSignVo vo) {
        if (!"GP_00".equals(vo.getCode())) {
            return vo.getMsg();
        }
        if (!"GPBIZ_00".equals(vo.getBiz_code())) {
            return vo.getBiz_msg();
        }

        return "success";
    }
}